package com.xy.p2p.action.front;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.annotation.ModelAndViewResolver;

import com.xy.common.util.DictUtils;
import com.xy.core.web.WebErrors;
import com.xy.p2p.model.Timageinfo;
import com.xy.p2p.model.Tmember;
import com.xy.p2p.service.TimageinfoService;
import com.xy.p2p.service.TmemberService;



@Controller
public class TimageinfoAction{
	private static final Logger log = LoggerFactory.getLogger(TimageinfoAction.class);
	public TimageinfoAction() {
	}
 
	@RequestMapping("/member/v_timageinfoupdoad.jspx")
	public String updoad(HttpServletRequest request, ModelMap model) {
		Tmember tmember = (Tmember) request.getSession().getAttribute(TmemberAction.AUTH_KEY);
		tmember = tmemberService.get(tmember.getMemberid());
		Timageinfo timageinfo = new Timageinfo(tmember, null, null, null, null);
		List<Timageinfo> list = timageinfoService.getTimageinfo(toMap(timageinfo));
		model.addAttribute("tiList", list);
		if (validateNull(tmember)) {
			model.addAttribute("validateTmember", 1);
		} else if (tmember.getEmailstatus() == 0) {
			model.addAttribute("validateTmember", 2);
		} else {
			model.addAttribute("validateTmember", 0);
		}
		setDict(model);
		// 用于导航栏
		model.addAttribute("dis", 15);
		return "/front/member/memberInfoUpload.html";
	}
	
	private boolean validateNull(Tmember tmember){
		return null == tmember.getPhone() ||"".equals(tmember.getPhone())||null == tmember.getIdcard() ||"".equals(tmember.getIdcard())||null == tmember.getEmail() ||"".equals(tmember.getEmail());
	}


 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param timageinfo
	 * @return
	 */	private Map<String,Object> toMap(Timageinfo timageinfo){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(timageinfo.getTiid()!=null&&!timageinfo.getTiid().equals("")){
			map.put("Tiid", timageinfo.getTiid());
		}
		if(timageinfo.getTmember()!=null){
			map.put("Tmember", timageinfo.getTmember());
		}
		if(timageinfo.getPath()!=null&&!timageinfo.getPath().equals("")){
			map.put("Path", timageinfo.getPath());
		}
		if(timageinfo.getContent()!=null&&!timageinfo.getContent().equals("")){
			map.put("Content", timageinfo.getContent());
		}
		if(timageinfo.getRemark()!=null&&!timageinfo.getRemark().equals("")){
			map.put("Remark", timageinfo.getRemark());
		}
		if(timageinfo.getStatus()!=null&&!timageinfo.getStatus().equals("")){
			map.put("Status", timageinfo.getStatus());
		}      
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
		model.addAttribute("statusMap", DictUtils.getDictByType(DictUtils.TIMAGEINFO_STATUS));
	}

	/**
	 * 数据校验
	 * @param model
	 */
	private WebErrors validateSave(Timageinfo timageinfo, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		/**if (errors.ifOutOfLength(username, "username", 1, 100)) {
			return errors;
		}  */    
		return errors;
	}

   

	@Resource
	private TimageinfoService timageinfoService;   
	@Resource
	private TmemberService tmemberService;   
}